package dev.polls.skycube;

import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.LinearLayout.LayoutParams;


public class Dropdown extends Animation
{
    private View AnimatedView;
    private LayoutParams lp;
    private int mMarginStart, mMarginEnd;
    private boolean mIsVisibleAfter = false;
    private boolean mWasEndedAlready = false;
    
    /*
     * Constructor
     * View = de view die moet expanden/collapsen
     * Duration is de tijd in milliseconde
     */
    public Dropdown(View view, int duration)
    {
    	
    	setDuration(duration);
        AnimatedView = view;
        lp = (LayoutParams) view.getLayoutParams();

        mIsVisibleAfter = (lp.bottomMargin == 0);

        mMarginStart = lp.bottomMargin;
        mMarginEnd = (mMarginStart == 0 ? (0- view.getHeight()) : 0);

        view.setVisibility(View.VISIBLE);
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t)
    {
    	super.applyTransformation(interpolatedTime, t);

        if (interpolatedTime < 1.0f)
        {

          
        	lp.bottomMargin = mMarginStart
                    + (int) ((mMarginEnd - mMarginStart) * interpolatedTime);

           
            AnimatedView.requestLayout();
            
     
        }
        else if (!mWasEndedAlready)
        {
        	lp.bottomMargin = mMarginEnd;
            AnimatedView.requestLayout();

            if (mIsVisibleAfter)
            {
                AnimatedView.setVisibility(View.GONE);
            }
            
            mWasEndedAlready = true;
        }
    }
}
